package design.mode.chapter3.item3.versionPlus.reporter;

import design.mode.chapter3.item3.versionPlus.domain.RequestInfo;
import design.mode.chapter3.item3.versionPlus.Stroage.MetricsStorage;
import design.mode.chapter3.item3.versionPlus.aggregator.Aggregator;
import design.mode.chapter3.item3.versionPlus.domain.RequestStat;
import design.mode.chapter3.item3.versionPlus.viewer.StatViewer;

import java.util.List;
import java.util.Map;

public abstract class ScheduledReporter {
    protected MetricsStorage metricsStorage;
    protected Aggregator aggregator;
    protected StatViewer viewer;

    public ScheduledReporter(MetricsStorage metricsStorage, Aggregator aggregator, StatViewer viewer) {
        this.metricsStorage = metricsStorage;
        this.aggregator = aggregator;
        this.viewer = viewer;
    }

    protected void doStatAndReport(long startTimeInMillis, long endTimeInMillis) {
        long durationInMillis = endTimeInMillis - startTimeInMillis;
        Map<String, List<RequestInfo>> requestInfos = metricsStorage.getRequestInfos(startTimeInMillis, endTimeInMillis);
        Map<String, RequestStat> aggregate = aggregator.aggregate(requestInfos, durationInMillis);
        viewer.output(aggregate, startTimeInMillis, endTimeInMillis);
    }


}
